package org.jeecg.modules.designer.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.extbpm.process.entity.ExtActProcess;
import org.jeecg.modules.extbpm.process.mapper.ExtActProcessMapper;
import org.jeecg.modules.extbpm.process.service.IExtActProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @ClassName: DesignerController
 * @Description: 流程设计器（负责页面跳转）
 * @author scott
 * @date 2010-03-12
 * 
 */
@Controller("designerController")
@RequestMapping("/act/designer")
public class DesignerController {
	private static final Log logger = LogFactory.getLog(DesignerController.class);
	@Autowired
	private IExtActProcessService extActProcessService;
	@Autowired
	private ExtActProcessMapper extActProcessMapper;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    @Lazy
    private RedisUtil redisUtil;
	/**
	 * 流程设计主页
	 * 
	 * @return
	 */
	@RequestMapping
	public void index(HttpServletRequest request,HttpServletResponse response) {
		String processid = oConvertUtils.getString(request.getParameter("id"), "0");
		String token = oConvertUtils.getString(request.getParameter("token"));
		logger.info(" index 登录令牌token： "+token);
        //校验 token 登录有效性
        TokenUtils.verifyToken(request, sysBaseAPI, redisUtil);

		request.setAttribute("processid", processid);// 流程ID
		request.setAttribute("token", token);// 登录人token令牌
		try {
			request.getRequestDispatcher("/designer/index.jsp").forward(request,response);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 流程设计属性页面跳转
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("processProperties")
	public void processProperties(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String turn = oConvertUtils.getString(request.getParameter("turn"));
		String id = oConvertUtils.getString(request.getParameter("id"));// 流程当前节点Id
		String checkbox = oConvertUtils.getString(request.getParameter("checkbox"));// 设置单选多选
		String processId = oConvertUtils.getString(request.getParameter("processId"));// 流程ID
		//TODO 查看流程属性是请求中流程id传参传给该属性了  后期需要优化
        String processDefinitionId = oConvertUtils.getString(request.getParameter("processDefinitionId"));// 流程ID
		logger.info(" 流程设计属性页面 turn： " + turn + ",节点ID: " + id + ",checkbox: " + checkbox + ",processId: " + processId);

		//设置流程类型
		ExtActProcess  process = extActProcessService.getById(processId);
		if(process!=null) {
			request.setAttribute("typeId", process.getProcessType());// 流程ID
		}else {
			process = extActProcessService.getById(processDefinitionId);
			if(process!=null) {
				request.setAttribute("typeId", process.getProcessType());// 流程ID
			}else{
				//默认测试流程
				request.setAttribute("typeId", "test");
			}
		}
		request.setAttribute("checkbox", checkbox);
		request.setAttribute("id", id);
		//流程设计器流程类型设置
		request.setAttribute("proTypeList", extActProcessMapper.getDictItems("bpm_process_type"));
		request.setAttribute("processId", processId);
		request.getRequestDispatcher("/designer/" + turn+".jsp").forward(request,response);
	}

	/**
	 * 选择表达式页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("goExpression")
	public void goExpression(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String id = oConvertUtils.getString(request.getParameter("id"));// 流程当前节点Id
		String checkbox = oConvertUtils.getString(request.getParameter("checkbox"));// 设置单选多选
		String processId = oConvertUtils.getString(request.getParameter("processId"));// 流程ID
		request.setAttribute("checkbox", checkbox);
		request.setAttribute("id", id);
		request.setAttribute("processId", processId);

		String token = oConvertUtils.getString(request.getParameter("token"));
		logger.debug(" index 登录令牌token： "+token);
		request.setAttribute("token", token);// 登录人token令牌
		request.getRequestDispatcher("/designer/selectExpressionList.jsp").forward(request,response);
	}

	/**
	 * popup选择用户列表页面
	 *
	 * @return
	 */
	@RequestMapping("goSearchSelectUser")
	public void goSearchSelectUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String id = oConvertUtils.getString(request.getParameter("id"));// 流程当前节点Id
		String checkbox = oConvertUtils.getString(request.getParameter("checkbox"));// 设置单选多选
		String processId = oConvertUtils.getString(request.getParameter("processId"));// 流程ID
		request.setAttribute("checkbox", checkbox);
		request.setAttribute("id", id);
		request.setAttribute("processId", processId);

		String token = oConvertUtils.getString(request.getParameter("token"));
		logger.debug(" index 登录令牌token： "+token);
		request.setAttribute("token", token);// 登录人token令牌
		request.getRequestDispatcher("/designer/searchSelectUserList.jsp").forward(request,response);
	}


	/**
	 * 跳转到子流程参数配置页面
	 *
	 * @return
	 */
	@RequestMapping("goSubParameterList")
	public void subParameterList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String id = oConvertUtils.getString(request.getParameter("id"));// 流程当前节点Id
		String checkbox = oConvertUtils.getString(request.getParameter("checkbox"));// 设置单选多选
		String processId = oConvertUtils.getString(request.getParameter("processId"));// 流程ID
		request.setAttribute("checkbox", checkbox);
		request.setAttribute("id", id);
		request.setAttribute("processId", processId);
		request.getRequestDispatcher("/designer/subParameterList.jsp").forward(request,response);
	}

	/**
	 * 跳转到监听列表页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("goListeners")
	public void goListeners(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		// 获取流程监听IDS
		String typeid = oConvertUtils.getString(request.getParameter("typeid"));
		logger.info(" 流程监听类型 typeid： " + typeid);
		request.setAttribute("typeid", typeid);

		String token = oConvertUtils.getString(request.getParameter("token"));
		logger.debug(" index 登录令牌token： "+token);
		request.setAttribute("token", token);// 登录人token令牌
		request.getRequestDispatcher("/designer/selectProcessListenerList.jsp").forward(request,response);
	}
}
